﻿using System;
using System.Xml;
using System.Text.RegularExpressions;
using System.Reflection;
namespace Ajax.Common
{
    /// <summary>
    /// XML 的摘要说明。
    /// </summary>
    public class XML
    {

        #region 保存Web.config
        /// <summary>
        /// 保存Web.config
        /// </summary>
        /// <param name="strKeyName">节点名</param>
        /// <param name="strKeyValue">要保存的值</param>
        /// <param name="strWhich">哪个配置文件</param>
        public static void SaveSetting(string strKeyName, string strKeyValue, string strWhich)
        {
            //创建XML文档实例
            System.Xml.XmlDocument XMLWebSetting = new System.Xml.XmlDocument();

            //打开XML文档
            XMLWebSetting.Load(System.Web.HttpRuntime.AppDomainAppPath + "/" + strWhich);

            //查找节点所在位置
            System.Xml.XmlNodeList XmlNodeList = XMLWebSetting.SelectSingleNode("//appSettings").ChildNodes;

            foreach (System.Xml.XmlNode xn in XmlNodeList)
            {
                if (xn.Attributes["key"].InnerText == strKeyName)
                {
                    xn.Attributes["value"].InnerText = strKeyValue;
                    XMLWebSetting.Save(System.Web.HttpContext.Current.Server.MapPath("~/" + strWhich + ""));
                    break;
                }
            }
        }
        #endregion

        #region 读取 XML中指定节点值
        /// <summary>
        /// 读取 XML中指定节点值
        /// </summary>
        /// <param name="strKeyName"></param>
        /// <returns></returns>
        public static string GetConfig(string strKeyName)
        {
            return GetConfig(strKeyName, "Web.config");
        }

        /// <summary>
        /// 读取 XML中指定节点值
        /// </summary>
        /// <param name="strKeyName">节点键名</param>
        /// <returns>指定节点键值</returns>
        public static string GetConfig(string strKeyName, string strWhich)
        {
            string configpath;
            try
            {
                configpath = string.Concat(System.Web.HttpRuntime.AppDomainAppPath, @"\", strWhich);

            }
            catch
            {
                configpath = System.Windows.Forms.Application.StartupPath;

                if (configpath.EndsWith(@"\bin"))
                {
                    configpath = configpath.Substring(0, configpath.Length - 4);
                }
                configpath = string.Concat(configpath, @"\", strWhich);
            }

            using (XmlTextReader tr = new XmlTextReader(configpath))
            {
                while (tr.Read())
                {
                    if (tr.NodeType == XmlNodeType.Element)
                    {
                        if (tr.Name == "add" && tr.GetAttribute("key") == strKeyName)
                        {
                            return tr.GetAttribute("value");
                        }
                    }
                }
            }
            return null;
        }
        #endregion

        public static string ModelToXML(object model)
        {
            XmlDocument xmldoc = new XmlDocument();
            XmlElement ModelNode = xmldoc.CreateElement("Model");
            xmldoc.AppendChild(ModelNode);

            if (model != null)
            {
                foreach (PropertyInfo property in model.GetType().GetProperties())
                {
                    XmlElement attribute = xmldoc.CreateElement(property.Name);
                    if (property.GetValue(model, null) != null)
                        attribute.InnerText = property.GetValue(model, null).ToString();
                    else
                        attribute.InnerText = "[Null]";
                    ModelNode.AppendChild(attribute);
                }
            }

            return xmldoc.OuterXml;
        }

        /// <summary>    
        /// XML转化为Model的方法    
        /// </summary>    
        /// <param name="xml">要转化的XML</param>    
        /// <param name="SampleModel">Model的实体示例，New一个出来即可</param>    
        /// <returns></returns>    
        public static object XMLToModel(string xml, object SampleModel)
        {
            if (string.IsNullOrEmpty(xml))
                return SampleModel;
            else
            {
                XmlDocument xmldoc = new XmlDocument();
                xmldoc.LoadXml(xml);

                XmlNodeList attributes = xmldoc.SelectSingleNode("Model").ChildNodes;
                foreach (XmlNode node in attributes)
                {
                    foreach (PropertyInfo property in SampleModel.GetType().GetProperties())
                    {
                        if (node.Name == property.Name)
                        {
                            if (node.InnerText != "[Null]")
                            {
                                if (property.PropertyType == typeof(System.Guid))
                                    property.SetValue(SampleModel, new Guid(node.InnerText), null);
                                else
                                    property.SetValue(SampleModel, Convert.ChangeType(node.InnerText, property.PropertyType), null);
                            }
                            else
                                property.SetValue(SampleModel, null, null);
                        }
                    }
                }
                return SampleModel;
            }
        }

    }
}